package org.os.framework.context.utils;

/**
 * 数学工具
 * 
 * @author
 */
public class MathUtil {

    private static final String PREFIX = "0b";

    public static Integer parseInt(String binary) {
        if (null == binary || "".equals(binary)) {
            return -1;
        }
        if (binary.startsWith(PREFIX)) {
            binary = binary.substring(2);
        }
        return Integer.parseInt(binary, 2);
    }

    public static Integer and(Integer a, Integer b) {
        return a & b;
    }

    public static Integer or(Integer a, Integer b) {
        return a | b;
    }

    public static String convert(Integer num, Integer len) {
        if (null == num) {
            return "";
        }
        String binary = Integer.toBinaryString(num);
        return String.format("%" + len + "s", binary).replace(" ", "0");
    }

    public static String convert0b(Integer num, Integer len) {
        String str = convert(num, len);
        if ("".equals(str)) {
            return "";
        }
        return "0b" + str;
    }

}
